﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Core.DS;

namespace Web.Controllers
{
    public partial class datastructController : JsonController
    {
        public ActionResult getDSProblemList(int eid)
        {
            return Json(problem.getDSProblemList(eid, Certify.username, Certify.teacherUp));
        }

        public ActionResult showDSProblem(int eid, int pid)
        {
            Sql.DSProblemStatistics ps;
            var p = problem.showDSProblem(eid, pid, out ps, Certify.teacherUp);
            if (p == null) return Content("题目不存在");
            ViewData["ps"] = ps;
            return View(p);
        }

        [ValidateInput(false)]
        [OnlyTeacherUp]
        public ActionResult addOrEditDSProblem(int eid, int? oldPid, int pid, string title, int timeLimit, int memoryLimit, string disabled,
            string description, string input, string output, string sampleInput, string sampleOutput, string hint, string source,
            int? exampleLang, string exampleCode, string inputData, string outputData,
            string uploadDataFile, HttpPostedFileBase inputDataFile, HttpPostedFileBase outputDataFile,
            string specialJudge, int? specialLang, string specialCode)
        {
            IsFileUpload = true;
            CheckEmpty(title, description, input, output, sampleInput, sampleOutput);
            if (exampleLang != null) CheckEmpty(exampleCode);
            var _uploadDataFile = uploadDataFile.ToBool();
            var _specialJudge = specialJudge.ToBool();
            if (_uploadDataFile)
            {
                CheckNull(inputDataFile, outputDataFile);
                inputData = new System.IO.StreamReader(inputDataFile.InputStream).ReadToEnd();
                outputData = new System.IO.StreamReader(outputDataFile.InputStream).ReadToEnd();
            }
            else
            {
                CheckEmpty(inputData, outputData);
                if (_specialJudge) CheckEmpty(specialLang, specialCode);
            }
            if (!inputData.EndsWith("\n")) inputData += "\r\n";
            if (!outputData.EndsWith("\n")) outputData += "\r\n";

            return Json(problem.addOrEditDSProblem(eid, oldPid, pid, title, timeLimit, memoryLimit, disabled.ToBool(),
                description.TrimHtmlEnd(), input.TrimHtmlEnd(), output.TrimHtmlEnd(), sampleInput, sampleOutput, hint.TrimHtmlEnd().ToHtmlQ(), source.ToQ(),
                exampleLang, exampleCode, inputData, outputData, _specialJudge, specialLang, specialCode));
        }

        [OnlyTeacherUp]
        public ActionResult getDSProblemCompleteInfo(int eid, int pid)
        {
            return Json(problem.getDSProblemCompleteInfo(eid, pid));
        }

        [OnlyTeacherUp]
        public ActionResult getDSAutoPid(int eid)
        {
            return Json(problem.getDSAutoPid(eid));
        }

        [OnlyTeacherUp]
        public ActionResult deleteDSProblem(int eid, int pid)
        {
            return Json(problem.deleteDSProblem(eid, pid));
        }
        [OnlyTeacherUp]
        public ActionResult swapProblem(bool flags,int pid,int eid)
        {
            return Json(problem.swapProblem(flags, pid, eid));
        }
    }
}
